var module = {
    URL: {
        imgUploadUrl: "/file/upload",//图片上传接口
        previewUrl: function (title, content) {
            return "/message/preview?title=" + title + "&content=" + content;
        },
        saveUrl: function (id) {
            if (id) {
                return "/message/update";
            } else {
                return "/message/save";
            }
        }  //信息保存接口
    },
    init: function () {
        layui.use(['layedit', 'layer'], function () {
            var layedit = layui.layedit
                , $ = layui.jquery, layer = layui.layer;

            //设置图片上传接口
            layedit.set({
                uploadImage: {
                    url: module.URL.imgUploadUrl //接口url
                }
            });
            //构建一个默认的编辑器
            var index = layedit.build('LAY_demo1');

            /**
             *@description 预览
             *@author XieXing
             *@date 2020/12/2 15:07
             */
            $(".preview").click(function () {
                layer.alert("功能正在赶来路上...", {icon: 6});
                return false;
                var title = $(".layui-title").val();
                var type = $(".msg-type").val();
                var content = layedit.getContent(index);
                if (!module.verify(title, type, content)) {
                    return false;
                }
                module.edit_model("dataModel", title, content)
            });

            /**
             *@description 消息提交
             *@author XieXing
             *@date 2020/11/27 14:21
             */
            $(".submit").click(function () {
                var title = $(".layui-title").val();
                var type = $(".msg-type").val();
                var content = layedit.getContent(index);
                if (!module.verify(title, type, content)) {
                    return false;
                }
                var id = $("#msgId").attr("v");//消息id
                $.post(module.URL.saveUrl(id), {
                    id: id,
                    msgTitle: title,
                    msgType: type,
                    msgContent: content
                }, function (result) {
                    if (result.code == 200) {
                        layer.msg("保存成功", {icon: 1});
                        location.href = "/message/";
                    } else {
                        layer.alert(result.msg);
                        return false;
                    }
                })
            })


        });
    },
    verify: function (title, type, content) {
        if (!title) {
            layer.msg("请输入标题", {icon: 2});
            return false;
        }
        if (!type) {
            layer.msg("请选择类型", {icon: 2});
            return false;
        }
        if (!content) {
            layer.msg("请输入内容", {icon: 2});
            return false;
        }
        return true;
    },
    /**
     *@description 编辑弹出层
     *@author XieXing
     *@date 2020/8/18 22:00
     */
    edit_model: function (modelName, title, content) {
        layer.open({
            type: 1,
            area: ['100%', '100%'],
            title: modelName,
            content: $("#dataModel"),
            offset: ['20px'],
            btn: ['提交', '取消'],
            btn2: function (index, layero) {
                layer.closeAll();
                return false;
            },
            cancel: function (layero, index) {
                layer.closeAll();
                return false
            },
            success: function (layero, index) {

            }
        });
    }

};
module.init();